package com.crm.service.impl.cluePoolManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.cluePoolManager.CluePoolRuleMapper;
import com.crm.model.entity.cluePoolManager.CluePoolRule;
import com.crm.model.entity.cluePoolManager.CluePoolRuleExample;
import com.crm.service.cluePoolManager.CluePoolRuleDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author yincuimei
 * @Date 2020/12/24 11:17
 */
@Service
public class CluePoolRuleDbServiceImpl implements CluePoolRuleDbService {

    @Autowired
    CluePoolRuleMapper cluePoolRuleMapper;

    @Override
    public Boolean insertCluePoolRule(CluePoolRule cluePoolRule) {
        Boolean result = false;
        int num = cluePoolRuleMapper.insertSelective(cluePoolRule);
        if(num>0){
            result = true;
        }
        return result;
    }

    @Override
    public List<CluePoolRule> selectCluePoolRuleByCluePoolId(String cluePoolId) {
        CluePoolRuleExample cluePoolRuleExample = new CluePoolRuleExample();
        CluePoolRuleExample.Criteria criteria = cluePoolRuleExample.createCriteria();
        criteria.andCluePoolIdEqualTo(cluePoolId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        return cluePoolRuleMapper.selectByExample(cluePoolRuleExample);
    }

    public Boolean deleteCluePoolRuleByCluePoolId(String cluePoolId){
        CluePoolRuleExample cluePoolRuleExample = new CluePoolRuleExample();
        CluePoolRuleExample.Criteria criteria = cluePoolRuleExample.createCriteria();
        criteria.andCluePoolIdEqualTo(cluePoolId);
        CluePoolRule cluePoolRule = new CluePoolRule();
        cluePoolRule.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        return cluePoolRuleMapper.updateByExampleSelective(cluePoolRule, cluePoolRuleExample) > 0;
    }

    /**
     * @author wll
     * info 查询当前线索池规则信息
     */
    @Override
    public CluePoolRule selectCluePoolMaxReceive(String poolId) {
        CluePoolRuleExample cluePoolRuleExample = new CluePoolRuleExample();
        CluePoolRuleExample.Criteria criteria = cluePoolRuleExample.createCriteria();
        criteria.andCluePoolIdEqualTo(poolId);//线索池id
        criteria.andRuleTypeEqualTo(1);//分配方式：1-领取；2-退回；3-分配
        criteria.andDeleteStatusEqualTo(Short.parseShort("0"));//删除状态---0: 正常; 1: 已删除
        List<CluePoolRule> cluePoolRules = cluePoolRuleMapper.selectByExample(cluePoolRuleExample);
        if(null != cluePoolRules && cluePoolRules.size()>0){
            return cluePoolRules.get(0);
        }
        return null;
    }

}
